package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.entity.AppPackage;
import la.iok.hzsvn.lewin.movie.model.AppPackageItemVo;
import la.iok.hzsvn.lewin.movie.model.AppPackageParam;
import la.iok.hzsvn.lewin.movie.model.Version;
import la.iok.hzsvn.lewin.movie.service.AppPackageItemService;
import la.iok.hzsvn.lewin.movie.service.AppPackageService;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

@RestController
@RequestMapping("appPackages")
public class AppPackageController {
    private final AppPackageService appPackageService;
    private final AppPackageItemService appPackageItemService;

    public AppPackageController(AppPackageService appPackageService,
                                AppPackageItemService appPackageItemService) {
        this.appPackageService = appPackageService;
        this.appPackageItemService = appPackageItemService;
    }

    @PostMapping
    public SingleDataResponse<Long> save(@RequestHeader Token token, @RequestBody AppPackageParam param){
        return new SingleDataResponse<>(appPackageService.save(token,param));
    }

    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token,@PathVariable Long id){
        appPackageService.delete(token,id);
        return OperatorResponses.success();
    }

    @PutMapping("{id}")
    public OperatorResponse latest(@RequestHeader Token token,@PathVariable Long id){
        appPackageService.latest(token,id);
        return OperatorResponses.success();
    }

    @GetMapping("{appName}/{packageType}/{productType}")
    public SingleDataResponse<Version> version(@PathVariable String appName,
                                               @PathVariable String packageType, @PathVariable String productType){
        return new SingleDataResponse<>(appPackageService.version(appName,packageType,productType));
    }

    @GetMapping
    public SingleDataResponse<PageVo<AppPackage>> page(int page, int limit){
        PageVo<AppPackage> data = appPackageService.page(page,limit);
        return new SingleDataResponse<>(data);
    }

    @GetMapping("{id}/files")
    public ListDataResponse<AppPackageItemVo> appPackages(@PathVariable Long id){
        return new ListDataResponse<>(appPackageItemService.packageFiles(id).stream().map(item->{
            AppPackageItemVo vo = new AppPackageItemVo();
            BeanUtils.copyProperties(item,vo);
            return vo;
        }).collect(Collectors.toList()));
    }

    @GetMapping("{id}/zip")
    public void zipPackage(HttpServletResponse response, @PathVariable Long id){
        appPackageService.downloadZipPackage(response,id);
    }
}
